package com.anrong.wulansdk.sdk.utils;

import java.util.Map;

import com.anrong.gson.Gson;
import com.anrong.gson.GsonBuilder;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * 偏好设置工具类
 * @author cxy
 *
 */
public class PreferenceUtil {
	
	public static final String CURRENT_USER = "pre_current_user";
	
	
	private static final String PREFERENCE_NAME = "wl_preference_name";
	
	private SharedPreferences sp;
	
	private static SharedPreferences getSP(Context ctx) {
		return ctx.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
	}
	
	public static void putString(Context ctx, String key, String value){
		Editor edit = getSP(ctx).edit();
		edit.putString(key, value);
		edit.commit();
	}

	
	public static String getString(Context ctx, String key){
		return getSP(ctx).getString(key, "");
	}
	
	
	public static void putMap(Context ctx, String key, Map<String, String> map){
		Editor edit = getSP(ctx).edit();
//		JSONObject obj = new JSONObject();
//		for (Map.Entry<String, String> entry : map.entrySet()) {
//			try {
//				obj.put(entry.getKey(), entry.getValue());
//			} catch (JSONException e) {
//				e.printStackTrace();
//			}
//		}
//		edit.putString(key, obj.toString());
		
		Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
		String json = gson.toJson(map);
		edit.putString(key, json);
		edit.commit();
	}
	
	public static Map<String, String> getMap(Context ctx, String key){
		String string = getSP(ctx).getString(key, "");
		Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
		return  gson.fromJson(string, Map.class);
	}
}
